<?php
namespace Home\Controller;
use Think\Controller;
class WowController extends Controller {
    public function index(){
        $this->assign('URL_PREFIX',C("URL_PREFIX"));
        $this->display(T('Index/Wow/index'));
    }
    //获取服务器
    public function getRealms(){
        $realmsForm = M('xwl_wow_realms');
        //先清空列表
        $realmsForm->where('1')->delete();
        //重新创建列表
        $realmStr = httpRequest("http://www.battlenet.com.cn/api/wow/realm/status");
        $realmList = json_decode($realmStr);
        $dataList = array();
        foreach($realmList->realms as $item){
            $id = date("s" ,time()).floor(microtime()*100000);
            foreach($item->connected_realms as $name){
                $temp = false;
                foreach($dataList as $data){
                    if($data['name'] == $name){
                        $temp = true;
                        break;
                    }
                }
                if($temp) continue;
                $dataList[] = array('name' => $name ,'id' => $id);
            }
        }
        $result = $realmsForm->addAll($dataList);
        //判断是否新增或修改成功;
        if($result) {
            $this->success('Success');
        }else{
            $this->error('Error');
        }
    }
    //获取物品信息
    public function getItem($id){
        $itemStr = httpRequest("http://www.battlenet.com.cn/api/wow/item/".$id);
        $itemList = json_decode($itemStr);
        $Form = M('xwl_wow_items');
        $data = $Form->where('id = '.$id)->find();
        //只记录可交易物品
        if($itemList->isAuctionable == 1 && $data == null){
            $data['id'] = $id;
            $data['name'] = $itemList-> name;
            $Form->add($data);
            $this->ajaxReturn(true);
        }
    }
    //获取服务器拍卖行信息
    public function getAuctionInfo($realmId){
        $realmsForm = M('xwl_wow_realms');
        $data = $realmsForm->where('id = '.$realmId)->group('id')->find();
        $auctionStr = httpRequest("http://www.battlenet.com.cn/api/wow/auction/data/".$data['name']);
        $productStr = httpRequest(json_decode($auctionStr)->files[0]->url);
        $productList = json_decode($productStr);
        $updateTime = json_decode($auctionStr)->files[0]->lastModified;
        $updateTime = date("Y-m-d H:i:s" ,substr($updateTime,0,10));
        $auctions = M('xwl_wow_auctions');
        $array = array();
        $index = 0;
        foreach($productList->auctions as $product){
            $index ++;
            $array[] = array('item' => $product->item ,'buyout' => $product->buyout,"realmId"=> $realmId);
            if($index == 500){
                $auctions->addAll($array);
                $array = array();
                $index = 0;
                //sleep(2);
            }
        }
        $auctions->addAll($array);
    }
    //拍卖行Job
    public function auctionJob(){
        $realmsForm = M('xwl_wow_realms');
        $data = $realmsForm->group('id')->select();
        foreach($data as $i){
            $this->getAuctionInfo($i['id']);
        }
    }
}
